﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Common;

//45集开始
public class GameFacade : MonoBehaviour
{
    private static GameFacade _instance;
    public static GameFacade Instance { get { return _instance; } }

    private UIManager uiMgr;
    private AudioManager audioMgr;
    private RequestManager requestMgr;
    private CameraManager cameraMgr;
    private PlayerManager playerMgr;
    private ClientManager clientMgr;

    private void Awake()
    {
        if (_instance != null)
        {
            Destroy(this.gameObject);
            return;
        }
        _instance = this;

        Init();
    }

    private void Init()
    {
        uiMgr = new UIManager(this);
        audioMgr = new AudioManager(this);
        requestMgr = new RequestManager(this);
        cameraMgr = new CameraManager(this);
        playerMgr = new PlayerManager(this);
        clientMgr = new ClientManager(this);

        uiMgr.OnInit();
        audioMgr.OnInit();
        requestMgr.OnInit();
        cameraMgr.OnInit();
        playerMgr.OnInit();
        clientMgr.OnInit();
    }

    private void Update()
    {
        UpdateManager();
    }

    private void DestroyManager()
    {
        uiMgr.OnDestroy();
        audioMgr.OnDestroy();
        requestMgr.OnDestroy();
        cameraMgr.OnDestroy();
        playerMgr.OnDestroy();
        clientMgr.OnDestroy();
    }

    private void UpdateManager()
    {
        uiMgr.OnUpdate();
        audioMgr.OnUpdate();
        requestMgr.OnUpdate();
        cameraMgr.OnUpdate();
        playerMgr.OnUpdate();
        clientMgr.OnUpdate();
    }

    /// <summary>
    /// 添加request
    /// </summary>
    /// <param name="requestCode"></param>
    /// <param name="request"></param>
    public void AddRequest(ActionCode actionCode, BaseRequest request)
    {
        requestMgr.AddRequest(actionCode, request);
    }

    /// <summary>
    /// 移除request
    /// </summary>
    /// <param name="requestCode"></param>
    public void RemoveRequest(ActionCode actionCode)
    {
        requestMgr.RemoveRequest(actionCode);
    }

    /// <summary>
    /// 相应服务器端的相应
    /// </summary>
    /// <param name="requestCode"></param>
    /// <param name="data"></param>
    public void HandleResponse(ActionCode actionCode, string data)
    {
        requestMgr.HandleResponse(actionCode, data);
    }

    /// <summary>
    /// 向服务器发送请求
    /// </summary>
    /// <param name="requestCode"></param>
    /// <param name="actionCode"></param>
    /// <param name="data"></param>
    public void SendRequest(RequestCode requestCode, ActionCode actionCode, string data)
    {
        clientMgr.SendRequest(requestCode, actionCode, data);
    }

    /// <summary>
    /// 显示Message面板
    /// </summary>
    /// <param name="msg"></param>
    public void ShowMessage(string msg)
    {
        uiMgr.ShowMessage(msg);
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name="soundName"></param>
    public void PlayBgSound(string soundName)
    {
        audioMgr.PlayBGSound(soundName);
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="soundName"></param>
    public void PlayNormalSound(string soundName)
    {
        audioMgr.PlayNormalSound(soundName);
    }

    public void SetUserData(UserData ud)
    {
        playerMgr.UserData = ud;
    }

    /// <summary>
    /// 玩家数据
    /// </summary>
    public UserData UserData
    {
        get { return playerMgr.UserData; }
    }

    private void OnDestroy()
    {
        DestroyManager();
    }
}
